using System.Collections.Generic;
using System.Data.SqlClient;
using TestSystem.DAL.TestSystem;


namespace TestSystem.BLL.TestSystem
{
    public class Answer : BusinessEntity
    {
        #region private fields
        private int _answerID;
        private int _testID;
        private string _body;
        private int _answerOrder;
        private bool _isCorrect;
        #endregion

        #region public prop and methods
        public int AnswerID
        {
            get { return _answerID; }
            set { _answerID = value; }
        }

        public int TestID
        {
            get { return _testID; }
            set { _testID = value; }
        }

        public string Body
        {
            get { return _body; }
            set { _body = value; }
        }

        public int AnswerOrder
        {
            get { return _answerOrder; }
            set { _answerOrder = value; }
        }

        public bool IsCorrect
        {
            get { return _isCorrect; }
            set { _isCorrect = value; }
        }

        private Test _test = null;
        public Test Test
        {
            get
            {
                if (_test == null)
                    _test = Test.GetByTestID(this.TestID);
                return _test;
            }
        }

        public override void Update()
        {
            AnswerDataAccess.Update(this.AnswerID, this.TestID, this.Body, this.AnswerOrder, this.IsCorrect);
        }

        protected override void Load(System.Data.IDataReader rdr)
        {
            AnswerID = (int) rdr["AnswerID"];
            TestID = (int) rdr["TestID"];
            Body = AnswerDataAccess.FromDBValue<string>(rdr["Body"]);
            AnswerOrder = (int) rdr["AnswerOrder"];
            IsCorrect = (bool) rdr["IsCorrect"];
        }
        #endregion

        #region static methods
        public static List<Answer> GetByTestID(int testID, int quizID)
        {
            Dictionary<int, List<Answer>> allAnswers = GetByQuizID(quizID);
            return allAnswers.ContainsKey(testID) ? allAnswers[testID] : null;
        }

        public static List<Answer> GetCorrectByTestID(int testID, int quizID)
        {
            List<Answer> allAnswers = GetByTestID(testID, quizID);
            return allAnswers.FindAll(delegate(Answer ans)
                                      {
                                          return ans.IsCorrect;
                                      });
        }

        public static int Add(int testID, string body, int answerOrder, bool isCorrect)
        {
            int id = AnswerDataAccess.Insert(testID, body, answerOrder, isCorrect);
            RemoveCacheItems("Answer_QuizAnswers_");
            return id;
        }

        public static int Add(SqlTransaction tran, int testID, string body, int answerOrder, bool isCorrect)
        {
            int id = AnswerDataAccess.Insert(tran, testID, body, answerOrder, isCorrect);
            RemoveCacheItems("Answer_QuizAnswers_");
            return id;
        }

        public static void RemoveByTestID(int testID)
        {
            AnswerDataAccess.DeleteAnswersByTestID(testID);
            RemoveCacheItems("Answer_QuizAnswers_");
            RemoveCacheItems("Answer_Answer_");
        }

        public static int GetCountByTestID(int testID)
        {
            return AnswerDataAccess.GetAnswersCountByTestID(testID);
        }

        public static int GetCorrectCountByTestID(int testID)
        {
            return AnswerDataAccess.GetCorrectAnswersCountByTestID(testID);
        }

        public static void Remove(int answerID)
        {
            AnswerDataAccess.Delete(answerID);
            RemoveCacheItems("Answer_QuizAnswers_");
            RemoveCacheItems("Answer_Answer_" + answerID);
        }

        public static List<Answer> GetAll()
        {
            return LoadList<Answer>(AnswerDataAccess.GetAll());
        }

        public static Answer GetByAnswerID(int answerID)
        {
            string key = "Answer_Answer_" + answerID;
            Answer answer = null;
            if (Cache[key] != null)
                answer = (Answer) Cache[key];
            else
            {
                answer = LoadEntity<Answer>(AnswerDataAccess.GetByAnswerID(answerID));
                CacheData(key, answer, 600, -1);
            }
            return answer;
        }

        private static Dictionary<int, List<Answer>> GetByQuizID(int quizID)
        {
            string key = "Answer_QuizAnswers_" + quizID;
            Dictionary<int, List<Answer>> ansDictionary = null;
            if (Cache[key] != null)
                ansDictionary = (Dictionary<int, List<Answer>>) Cache[key];
            else
            {
                ansDictionary = LoadAnswerDictionary(AnswerDataAccess.GetByQuizID(quizID));
                CacheData(key, ansDictionary, -1, 3600);
            }
            return ansDictionary;
        }

        public static int GetCount()
        {
            return AnswerDataAccess.GetCount();
        }
        #endregion
    }
}
